import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import {getItem} from '@/utils/storage'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'layout',
    component: () => import(/* webpackChunkName: "about" */ '@/layout'),
    children: [{
      path: '/kanban',
      name: 'kanban',
      component: () => import(/* webpackChunkName: "about" */ '@/views/kanban'),
    },
    {
      path: '/courseVisit',
      name: 'courseVisit',
      component: () => import(/* webpackChunkName: "about" */ '@/views/courses/courseVisit'),
    },
    {
      path: '/onlinePeople',
      name: 'onlinePeople',
      component: () => import(/* webpackChunkName: "about" */ '@/views/courses/onlinePeople'),
    },
    {
      path: '/subjectStat',
      name: 'subjectStat',
      component: () => import(/* webpackChunkName: "about" */ '@/views/courses/subjectStat'),
    },
    {
      path: '/subjectRank',
      name: 'subjectRank',
      component: () => import(/* webpackChunkName: "about" */ '@/views/courses/subjectRank'),
    },
    {
      path: '/frequencyDistribution',
      name: 'frequencyDistribution',
      component: () => import(/* webpackChunkName: "about" */ '@/views/courses/frequencyDistribution'),
    },
    {
      path: '/activeUser',
      name: 'activeUser',
      component: () => import(/* webpackChunkName: "about" */ '@/views/user/activeUser'),
    },
    {
      path: '/newUser',
      name: 'newUser',
      component: () => import(/* webpackChunkName: "about" */ '@/views/user/newUser'),
    }, {
      path: '/agentStat',
      name: 'agentStat',
      component: () => import(/* webpackChunkName: "about" */ '@/views/organ/agentStat'),
    }, {
      path: '/onlineDetail',
      name: 'onlineDetail',
      component: () => import(/* webpackChunkName: "about" */ '@/views/journal/onlineDetail'),
    }]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '@/views/login'),
    // meta: {
    //   needLogin: true
    // }
  }
]


const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path !== "/login") {
    if (getItem('hasToken')) {
      // console.log(store.state.token);

      next()
    } else {
      console.log(store.state.token);
      next('/login?url=' + to.path)
    }
  } else {
    next()
  }
})
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push (location, onResolve, onReject) {
//   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//   return originalPush.call(this, location).catch(err => err)
// }
export default router
